package starcoast.bocai.com.starcoast.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;


public class TextAdapter extends ArrayAdapter<String> {

	private Context mContext;
	private List<String> mListData;
	private String[] mArrayData;
	private String[] mArrayInt;
	private int selectedPos = -1;
	private String selectedText = "";
	private int normalDrawbleId;
	private int selectedDrawble;
	private float textSize;
	private OnClickListener onClickListener;
	private OnItemClickListener mOnItemClickListener;

	public TextAdapter(Context context, String[] arrayData, int sId, int nId) {
		super(context, R.string.no_data, arrayData);
		mContext = context;
		mArrayData = arrayData;
		selectedDrawble = sId;
		normalDrawbleId = nId;
		init();
	}
	public TextAdapter(Context context, String[] arrayData,String[] mArrayInt, int sId, int nId) {
		super(context, R.string.no_data, arrayData);
		mContext = context;
		mArrayData = arrayData;
		this.mArrayInt = mArrayInt;
		selectedDrawble = sId;
		normalDrawbleId = nId;
		init();
	}
	private void init() {
		onClickListener = new OnClickListener() {

			@Override
			public void onClick(View view) {
				selectedPos = (Integer) view.getTag();
				setSelectedPosition(selectedPos);
				if (mOnItemClickListener != null) {
					mOnItemClickListener.onItemClick(view, selectedPos);
				}
			}
		};
	}

	/**
	 * 设置选中的position,并通知列表刷新
	 */
	public void setSelectedPosition(int pos) {
		if (mListData != null && pos < mListData.size()) {
			selectedPos = pos;
			selectedText = mListData.get(pos);
			notifyDataSetChanged();
		} else if (mArrayData != null && pos < mArrayData.length) {
			selectedPos = pos;
			selectedText = mArrayData[pos];
			notifyDataSetChanged();
		}

	}

	/**
	 * 设置选中的position,但不通知刷新
	 */
	public void setSelectedPositionNoNotify(int pos) {
		selectedPos = pos;
		if (mListData != null && pos < mListData.size()) {
			selectedText = mListData.get(pos);
		} else if (mArrayData != null && pos < mArrayData.length) {
			selectedText = mArrayData[pos];
		}
	}
	/**
	 * 获取选中的position
	 */
	public int getSelectedPosition() {
		if (mArrayData != null && selectedPos < mArrayData.length) {
			return selectedPos;
		}
		if (mListData != null && selectedPos < mListData.size()) {
			return selectedPos;
		}
		return -1;
	}
	/**
	 * 设置列表字体大小
	 */
	public void setTextSize(float tSize) {
		textSize = tSize;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		View view;
		if (convertView == null) {
			convertView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.choose_item, parent, false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String mString = "";
		String iString = "";
		 if (mArrayData != null) {
			if (position < mArrayData.length) {
				mString = mArrayData[position];
			}
		}
		if (mArrayInt != null){
			 if (position < mArrayInt.length) {
				 iString = mArrayInt[position];
			 }
		 }
		if (mString.contains("不限"))
		{}
		else {
			viewHolder.tv1.setText(mString);
			if (mArrayInt!= null) {
				viewHolder.tv2.setVisibility(View.VISIBLE);
				viewHolder.tv2.setText(iString);
				viewHolder.tv1.setGravity(Gravity.CENTER_VERTICAL);
			}else{
				viewHolder.tv2.setVisibility(View.GONE);
				viewHolder.tv1.setGravity(Gravity.CENTER);
			}
		}
		viewHolder.tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize);
		viewHolder.tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize);

		if (selectedText != null && selectedText.equals(mString)) {
			viewHolder.tv1.setTextColor(getContext().getResources().getColor(R.color.colortitlebag));
			viewHolder.tv2.setTextColor(getContext().getResources().getColor(R.color.colortitlebag));
		} else {
			viewHolder.tv1.setTextColor(getContext().getResources().getColor(R.color.color_black));
			viewHolder.tv2.setTextColor(getContext().getResources().getColor(R.color.color_black));
		}
		convertView.setPadding(40, 10, 0, 10);
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedPosition(position);
				if (mOnItemClickListener != null) {
					mOnItemClickListener.onItemClick(v, selectedPos);
				}
			}
		});
		return convertView;
	}

	public void setOnItemClickListener(OnItemClickListener l) {
		mOnItemClickListener = l;
	}

	/**
	 * 重新定义菜单选项单击接口
	 */
	public interface OnItemClickListener {
		public void onItemClick(View view, int position);
	}
	class ViewHolder {
		@Bind(R.id.tv1)
		TextView tv1;
		@Bind(R.id.tv2)
		TextView tv2;
		ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}
}
